﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using WeChatDemo.Models;
using WeChatDemo.Models.Photo;
using WeChatDemo.Models.DTO;
using WeChatDemo.Models.DTO.Photo;
using WeChatDemo.DataProvider;
using WeChatDemo.DataProvider.Photo;

namespace WeChatDemo.Controllers
{
    public class PhotoItemController : ApiController
    {
        [HttpGet]
        public IHttpActionResult Get()
        {
            try
            {
                IEnumerable<DTOPhotoItem> _list = null;
                _list = DAOPhotoItemDetails.GetAllList("ORDER BY CreatedAt DESC");

                if (_list.Count<DTOPhotoItem>() == 0)
                {
                    return Ok(new { count = 0 });
                }
                else
                {
                    return Ok(new { count = _list.Count<DTOPhotoItem>(), results = _list});
                }
            }
            catch (Exception ex)
            {

                return InternalServerError(ex);
            }

        }

        [HttpGet]
        public IHttpActionResult Get([FromUri] string where)
        {
            try
            {
                IEnumerable<DTOPhotoItem> _list = null;
                _list = DAOPhotoItemDetails.GetList(where);

                if (_list.Count<DTOPhotoItem>() == 0)
                {
                    return Ok(new { count = 0 });
                }
                else
                {
                    return Ok(new { count = _list.Count<DTOPhotoItem>(), results = _list });
                }
            }
            catch (Exception ex)
            {

                return InternalServerError(ex);
            }
        }

        [HttpGet]
        public IHttpActionResult Get([FromUri] int pageSize, [FromUri] string where)
        {
            try
            {
                IEnumerable<DTOPhotoItem> _list = null;
                _list = DAOPhotoItemDetails.GetList(pageSize, where);

                if (_list.Count<DTOPhotoItem>() == 0)
                {
                    return Ok(new { count = 0 });
                }
                else
                {
                    return Ok(new { count = _list.Count<DTOPhotoItem>(), results = _list });
                }
            }
            catch (Exception ex)
            {

                return InternalServerError(ex);
            }
        }

        [HttpGet]
        public IHttpActionResult Get(Guid id)
        {
            IEnumerable<DTOPhotoItem> _list = null;
            _list = DAOPhotoItemDetails.GetList($"AND ID = '{id}'");

            return Ok(new { result = _list.SingleOrDefault<DTOPhotoItem>() });
        }

        [HttpPost]
        public IHttpActionResult Post([FromBody]PhotoItem model)
        {
            model.ID = Guid.NewGuid();
            
            if (DAOPhotoItem.Add(model))
            {
                return Created<PhotoItem>("success", model);
            }
            else
            {
                return InternalServerError();
            }
        }

        [HttpPut]
        public IHttpActionResult Put([FromBody]PhotoItem model)
        {
            if (DAOPhotoItem.Update(model))
            {
                return Ok<PhotoItem>(model);
            }
            else
            {
                return InternalServerError();
            }
        }

        [HttpDelete]
        public IHttpActionResult Delete(Guid id)
        {
            if (DAOPhotoItem.Delete(id))
            {
                return Ok(new { result = "success" });
            }
            else
            {
                return InternalServerError();
            }
        }
    }
}
